import Vue from 'vue'
import Router from 'vue-router'
import Main from '@/components/pages/main.vue'
import Auth from '@/components/pages/auth.vue'
import Sheets from '@/components/core/sheets.vue'
Vue.use(Router)

const router = new Router({
  routes: [
    {
        mode:'hash',
        path: '/',
        name: 'main',
        component: Main,
        meta: {
            //requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        },
        children:[
            {
                path:'sheets',
                component:Sheets,
                meta: {
                    //requireAuth: true,
                },
            }
        ]
    },
    {
        mode:'hash',
        path: '/login',
        name: 'login',
        component: Auth,
        
    }
  ]
});


//路由访问前的审核判定
router.beforeEach((to,from,next)=>{
    if(to.meta.requireAuth){
        if (localStorage.getItem('token')) {  // 通过vuex state获取当前的token是否存在
            next();
        }
        else {
            next({
                path: '/login',
                query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
            })
        }
    }else{
        next();
    }
})

export default router;

